import 'package:erp/Entity/ProductBalance.dart';
import 'package:erp/Entity/ProductDetailAtPurchaseOrSell.dart';
import 'package:erp/Entity/PurchaseOrder.dart';
import 'package:erp/Events/EventAddrs.dart';
import 'package:erp/Store/PurchaseStore.dart';
import 'package:erp/UI/CodeResources/Localization.dart';
import 'package:erp/UI/Components/EventfulState.dart';
import 'package:erp/UI/Components/KeyValueView.dart';
import 'package:erp/UI/Components/ListDividerGenerator.dart';
import 'package:erp/global.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';


class PurchaseOrderDetailPage extends StatefulWidget {
  final avatarRadius = 64.0;
  final String heroID;
  final listLabelTextStyle = TextStyle(fontSize: 14.0);
  final orderInfoLabelTextStyle=TextStyle(color: Colors.white);
  PurchaseOrder order;

  PurchaseOrderDetailPage(this.heroID, this.order);

  @override
  State<StatefulWidget> createState() => PurchaseOrderDetailPageState();
}

class PurchaseOrderDetailPageState extends EventfulState<PurchaseOrderDetailPage>{
  @override
  void initState() {
    eventIds = [
      globalBus.subscribe<List<ProductDetailAtPurchaseOrSell>>(Events.PURCHASE_ORDER_DETAIL_SELECT_SUCCESS, (bus, data){
        setState(() {
          widget.order.products = data;
        });
      })
    ];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    
    var sourceItems = widget.order.products?.map((item) => generateItem(context, item))?.toList() ?? [];
    if (sourceItems.isEmpty){
      globalBus.publish(Events.PURCHASE_ORDER_DETAIL_SELECT_REQUEST, widget.order);
    }
    var items = ListDividerGenerator(sourceItems, Divider()).items;
    var currentLocale = AppLocalizations.of(context);

    return Directionality(textDirection: currentLocale.textDirection, child: Scaffold(
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.close),
          onPressed: (){
            Navigator.of(context).pop();
          }
      ),
          body: CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
                pinned: true,
                expandedHeight: 320.0,
                flexibleSpace: FlexibleSpaceBar(
                  background: Center(
                    child: Column(

                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Hero(
                            tag: widget.heroID,
                            child: CircleAvatar(
                              backgroundColor: Colors.white,
                              radius: 64.0,
                              child: Image.asset("assets/pictures/list.png", height: (widget.avatarRadius * 2) - 18, width: (widget.avatarRadius * 2) - 18,),
                            )
                        ),
                        KeyValueView(
                          {
                            "تەمىنلىگۈچى: ": widget.order.supplierName ?? "",
                            "ئامبار: ": widget.order.products.firstWhere((item) => true, orElse: () => null)?.warehouseName ?? "",
                            "ۋاقتى: ": DateFormat("yyyy-MM-dd HH:mm:ss").format(widget.order.time),
                            "سوممىسى: ": widget.order.amount.toString(),
                            "بىجىرگۈچى: ": widget.order.employeeName,
                            "تالون نومۇرى: ": widget.order.orderNumber
                          },
                          textColor: Colors.white,
                          width: 300.0,
                        )
                      ],
                    ),
                  ),
                  title: Text("تەپسىلاتى"),
                  centerTitle: true,
                ),
              ),
              SliverList(delegate: SliverChildListDelegate(items))
            ],
          ),
    ));
    
  }

  Widget generateItem(BuildContext context, ProductDetailAtPurchaseOrSell product){
    return ExpansionTile(
      leading: Image.asset(
        "assets/pictures/box.png",
        height: 48.0,
        width: 48.0,
      ),
      title: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
//                Icon(Icons.people, color: primaryColor),
              Expanded(child: Text("مەھسۇلات نامى: ", style: widget.listLabelTextStyle,), ),
              Expanded(child: Text(product.nameUg, style: widget.listLabelTextStyle, textAlign: TextAlign.end), flex: 2,),
            ],
          ),
          Row(
            children: <Widget>[
//                Icon(Icons.access_time, color: primaryColor),
              Expanded(child: Text("سانى: ", style: widget.listLabelTextStyle)),
              Expanded(child: Text(product.count.toString(), style: widget.listLabelTextStyle, textAlign: TextAlign.end), flex: 2,),
            ],
          ),
          Row(
            children: <Widget>[
//                Icon(Icons.attach_money, color: primaryColor),
              Expanded(child: Text("نومۇرى: ", style: widget.listLabelTextStyle)),
              Expanded(child: Text(product.productCode, style: widget.listLabelTextStyle, textAlign: TextAlign.end,), flex: 2,),
            ],
          ),
        ],
      ),
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(top: 0.0, bottom: 10.0, right: 56.0, left: 56.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Icon(
                      Icons.attach_money,
                      color: Theme.of(context).primaryColor
                  ),
                  Expanded(child: Text("يەككە كىرىش باھاسى: ", )),
                  Expanded(child: Text(product.inPrice.toString(), textAlign: TextAlign.end), flex: 2,),
                ],
              ),
              Row(
                children: <Widget>[
                  Icon(
                      Icons.confirmation_number,
                      color: Theme.of(context).primaryColor
                  ),
                  Expanded(child: Text("تاياقچە كودى: ")),
                  Expanded(child: Text(product.barCode, textAlign: TextAlign.end), flex: 2,),
                ],
              ),
            ],
          ),
        )
      ],
      trailing: null,
//      backgroundColor: primaryColor.withAlpha(10),
    );
  }
}

